package uit.ce03.game_ninja.quaivat;

import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.os.SystemClock;




import android.os.SystemClock;

	public class Status_QuaiVat {
	public AnimatedSprite mSta_AnimatedSprite;
	
	private Scene mScene;

	public int speed = 2;
	
	private quaivat quaivat;
	
	private int loai = 0;
	//=============================================================================
	
	 public Status_QuaiVat(quaivat quaivat, Scene mScene,float pX, float pY, TiledTextureRegion Quaivat_TiledTextureRegion){
		this.quaivat = quaivat;
		this.mScene = mScene;	
		loai = getRandomIndex(0, 3);
		mSta_AnimatedSprite = new AnimatedSprite(pX, pY, Quaivat_TiledTextureRegion); 
		Status_moveLeft();
		mScene.attachChild(mSta_AnimatedSprite);
	}
	
	 
	public void Status_moveLeft()
	{
		if(loai == 0)
		{
			mSta_AnimatedSprite.animate(new long[]{100,100,100},12,14,true);
		}
		else if(loai == 1)
		{
			mSta_AnimatedSprite.animate(new long[]{100,100,100},15,17,true);
		}
		else if(loai == 2)
		{
			mSta_AnimatedSprite.animate(new long[]{100,100,100},18,20,true);
		}
		else if(loai == 3)
		{
			mSta_AnimatedSprite.animate(new long[]{100,100,100},21,23,true);
		}
		
	}
	public void Status_moveRight()
	{
		if(loai == 0)
		{
			mSta_AnimatedSprite.animate(new long[]{100,100,100},24,26,true);
		}
		else if(loai == 1)
		{
			mSta_AnimatedSprite.animate(new long[]{100,100,100},27,29,true);
		}
		else if(loai == 2)
		{
			mSta_AnimatedSprite.animate(new long[]{100,100,100},30,32,true);
		}
		else if(loai == 3)
		{
			mSta_AnimatedSprite.animate(new long[]{100,100,100},33,35,true);
		}
	}
	//=============================================================================
	
	public void moveXY(float pX, float pY){
		
		mSta_AnimatedSprite.setPosition(pX, pY);
	}
	//=============================================================================
	public void moveRelativeXY(float pX, float pY){
		mSta_AnimatedSprite.setPosition(mSta_AnimatedSprite.getX()+pX,mSta_AnimatedSprite.getY()+pY);
	}
	//=============================================================================
	
	long time = SystemClock.elapsedRealtime();
	int huong = 0;
	boolean run = false;
	boolean run1 = false;
	float x1,y1,x2,y2;
	public void moveRandom(){
		if(SystemClock.elapsedRealtime() - time > 2000){
			huong = getRandomIndex(0, 1);
			time = SystemClock.elapsedRealtime();
			if(huong == 0)
				Status_moveLeft();
			else if(huong == 1)
				Status_moveRight();
			
		}
		x1 = quaivat.toadoleftX(mSta_AnimatedSprite);
		y1 = quaivat.toadoleftY(mSta_AnimatedSprite);
		x2 = quaivat.toadorightX(mSta_AnimatedSprite);
		y2 = quaivat.toadorightY(mSta_AnimatedSprite);
		
		if(huong == 0){
			if(quaivat.checkLeft(x1, y1))
			{
				moveRelativeXY(-speed,0);
			}
			else
			{
				moveRelativeXY(speed,0);
			}
		}else if(huong == 1){
			if(quaivat.checkLeft(x2, y2))
			{
				moveRelativeXY(speed,0);
			}
			else
			{
				moveRelativeXY(-speed,0);
			}
			
				
		}
	}
	//=============================================================================
	
	public void deleteQuaivat_1(){
		this.mScene.detachChild(mSta_AnimatedSprite);
	}
	//============================================================================
	long time_reset_begin = 0;
	public boolean bool_reset = false;
	/*public void reset(){
		if(bool_reset){
			if(!this.mSta_AnimatedSprite.isVisible() && time_reset_begin == 0)
				time_reset_begin = SystemClock.elapsedRealtime();
			
			if(time_reset_begin != 0 && SystemClock.elapsedRealtime() - time_reset_begin > 2000){
				
				this.mSta_AnimatedSprite.setPosition(416,128);
				this.mSta_AnimatedSprite.setVisible(true);
				time_reset_begin = 0;
				bool_reset = false;
			}
		}
	}*/
	public static int getRandomIndex(int min, int max) {
		return (int) (Math.random() * (max - min + 1)) + min;
	}
}